﻿using Delonix_Regia_HMS.Classes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Delonix_Regia_HMS
{
    public partial class CreateAccount : Form
    {
        public CreateAccount()
        {
            InitializeComponent();
            DBManager.FillComboxBoxStaffID(cbxCreateAccountUsername);
        }

        //Create User Account
        private void btnCreateAccountCreate_Click(object sender, EventArgs e)
        {
            bool create = false;
            if (string.IsNullOrEmpty(cbxCreateAccountUsername.Text))
            {
                MessageBox.Show("You are Missing Staff ID!");
            }
            else if (string.IsNullOrEmpty(tbxCreateAccountPassword.Text))
            {
                MessageBox.Show("You are Missing Account Password!");
            }
            else if (string.IsNullOrEmpty(cbxCreateAccountStaffType.Text))
            {
                MessageBox.Show("You are Missing Staff type!");
            }
            else
            {
                StaffAccount sa = new StaffAccount();
                Staff s = new Staff();
                sa.Username = Convert.ToInt32(cbxCreateAccountUsername.SelectedItem.ToString());
                sa.Password = tbxCreateAccountPassword.Text;
                s.StaffType = cbxCreateAccountStaffType.SelectedItem.ToString();
                DBManager.CreateStaffAccount(sa, s);
                create = true;
            }
            if (create == true)
            {
                MessageBox.Show("Create Successful!");
                DBManager.FillComboxBoxStaffID(cbxCreateAccountUsername);
                ClearTextBoxes(this.Controls);
                ClearComboBoxes(this.Controls);
            }
        }

        //Clear textboxes after create
        private void ClearTextBoxes(Control.ControlCollection cc)
        {
            foreach (Control ctrl in cc)
            {
                TextBox tb = ctrl as TextBox;
                if (tb != null)
                {
                    tb.Text = "";
                }

                else
                {
                    ClearTextBoxes(ctrl.Controls);
                }
            }
        }

        //Clear ComboBoxes after create
        private void ClearComboBoxes(Control.ControlCollection cc)
        {
            foreach (Control ctrl in cc)
            {
                ComboBox cb = ctrl as ComboBox;
                if (cb != null)
                {
                    cb.Text = "";
                }

                else
                {
                    ClearComboBoxes(ctrl.Controls);
                }
            }
        }

        //Changes the staff name when another staff ID is selected
        private void cbxCreateAccountUsername_SelectedIndexChanged(object sender, EventArgs e)
        {
            tbxCreateAccountStaffName.Clear();
            string s = DBManager.RetrieveCustomerNameByStaffID(Convert.ToInt32(cbxCreateAccountUsername.SelectedItem.ToString()));
            tbxCreateAccountStaffName.Text = s;
        }
        
    }
}
